# -*- coding: utf-8 -*-
'''
Author: your name
Date: 2020-11-06 15:19:56
LastEditTime: 2021-08-09 16:43:45
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: /tinyms/tinyms-api/apps/auth/menu/views.py
'''
from flask import render_template,request
from app import db
from . import auth_menu
from .model import Menu
from .service import MenuService

from apps.utils.TypeUtils import TypeUtils
import json
import uuid
import time

@auth_menu.route('/saveUpdate',methods=['GET','POST'])
def saveUpdate():
    data = {}
    if request.method == "POST":
        for key in request.form:
            data[key] = request.form.get(key)
    else:
        for key in request.args:
            data[key] = request.args.get(key)
    menu = Menu()
    # menu.menu_pk = data['menu_pk']
    menu.menu_name = data['menu_name']
    menu.menu_icon = data['menu_icon']
    menu.menu_parent = data['menu_parent']
    menu.menu_url = data['menu_url']
    menu.menu_type = data['menu_type']
    menu.menu_number = data['menu_number']
    menu.menu_status = data['menu_status']
    menu.sys_type = data['sys_type']
    # menu.menu_default = data['menu_default']
        
    # if not data['menu_pk']:
    if not data.get('menu_pk'):
        # menu.menu_pk = str(uuid.uuid1())
        MenuService.save(menu)
    else:
        menu.menu_pk = data['menu_pk']
        MenuService.update(menu)
    data = {
        'msg':'保存成功',
        'code':0
    }
    return json.dumps(data,ensure_ascii=False)

@auth_menu.route('/getMenuInfo',methods=['GET','POST'])
def getMenuInfo():
    if request.method == "POST":
            menu_pk = request.form.get("menu_pk")  #获取页码
    else:
        menu_pk = request.args.get("menu_pk")
    data = MenuService.getMenuInfo(menu_pk)
    return json.dumps(TypeUtils.object_to_dict(data),ensure_ascii=False)


@auth_menu.route('/list',methods=['GET','POST'])
def select():
    #必须实例化，否则无法调用递归
    menservice = MenuService()
    menus = menservice.menu_list()
    data = {
        'msg':'success',
        'code':0,
        'data':menus
    }
    return json.dumps(data,ensure_ascii=False)
    # return data

#获取用户信息的分页列表
@auth_menu.route('/pages',methods=['GET','POST'])
def pages():
    if request.method == "POST":
        page = request.form.get("page")  #获取页码
    else:
        page = request.args.get("page")
    pages = MenuService.pages(page) #flask_sqlalchemy提供分页方法.paginate

    data = {
        'msg':'success',
        'code':0,
        'data':Menu.to_dict(pages.items),
        'count':pages.total
    }

    print(pages.items)
    return json.dumps(data,ensure_ascii=False)  #关闭已ascii编码输出


'''
 删除菜单
'''
@auth_menu.route('/delete',methods=['GET','POST'])
def delete():
    if request.method == "POST":
        menu_pk = request.form.get("menu_pk")  #获取页码
    else:
        menu_pk = request.args.get("menu_pk")
    MenuService.delete(menu_pk)
    data = {
        'msg':'删除成功',
        'code':0,
    }
    return json.dumps(data,ensure_ascii=False)

'''
查询具有权限的菜单列表
'''
@auth_menu.route('/auth_menu_list',methods=['GET','POST'])
def auth_menu_list():
    #必须实例化，否则无法调用递归
    menservice = MenuService()
    menus = menservice.menu_list()
    return menus
'''
查询具有权限的按钮列表
'''
@auth_menu.route('/auth_button_list',methods=['GET','POST'])
def auth_button_list():
    #必须实例化，否则无法调用递归
    if request.method == "POST":
        role_pk = request.form.get("role_pk")  #获取页码
    else:
        role_pk = request.args.get("role_pk")
    menus = MenuService.btns_checked(role_pk)
    data_dict = Menu.btns_to_array(menus)
    return json.dumps(data_dict,ensure_ascii=False)


'''
查询菜单列表包含按钮
用于角色授权时显示所有菜单及按钮
'''
@auth_menu.route('/tree_menu_list',methods=['GET','POST'])
def tree_menu_list():
    #必须实例化，否则无法调用递归
    menservice = MenuService()
    menus = menservice.list_all()
    return menus

'''
根据角色主键
查询角色授予的菜单及按钮
此处只查询那些菜单和按钮是选中的
为角色授权树形菜单回显选中的按钮和菜单
'''
@auth_menu.route('/role_menu_checked',methods=['GET','POST'])
def role_menu_checked():
    if request.method == "POST":
        role_pk = request.form.get("role_pk")  #获取页码
    else:
        role_pk = request.args.get("role_pk")
    menus = MenuService.list_checked(role_pk)
    data_dict = Menu.to_dict(menus)
    return json.dumps(data_dict,ensure_ascii=False)